﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Drawing.Printing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RichTextBoxCtrl;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.BusinessLogic.Common;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Client.UIL.ManageCommon;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.Static;
using OpenSoftStore.Orm.Status;

namespace OpenSoftStore.Client.UIL.ManageMember
{
    public partial class MemberAdd : UserControl
    {
        #region Fields

        public T_Member Tmember;

        public MemberLogic LgcMember;

        public ComboLogic LgcCombo;

        public MemberList UcMemberList;
        public MemberAddWindow WndMemberAdd;
        public AdminLogic LgcAdmin;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;
        #endregion

        public MemberAdd()
        {
            InitializeComponent();
            Tmember=new T_Member {AddTime = DateTime.Now, BrithDate = DateTime.Now};
            LgcMember = new MemberLogic();
            LgcCombo=new ComboLogic();

            txtCardId.Text=Tmember.CardId = LgcMember.GetCountNo("T_Member");
            LgcAdmin=new AdminLogic();

            var admins = LgcAdmin.GetAdminViewFast();
            cmbAdminId.DataSource = admins;
            cmbAdminId.DisplayMember = "Name";
            cmbAdminId.ValueMember = "Id";
            cmbAdminId.Text = S_Admin.Name;

            cmbSex.DisplayMember = "DisplayText";
            cmbSex.ValueMember = "Value1";
            cmbSex.DataSource = LgcCombo.GetComboByBuzType("Sex");

            cmbSkinType.DisplayMember = "DisplayText";
            cmbSkinType.ValueMember = "Value1";
            cmbSkinType.DataSource = LgcCombo.GetComboByBuzType("SkinType");
            //rchDocument.Height = 600;
            rchDocument.Height = 500;
        }
        private void txtCardId_Load(object sender, EventArgs e)
        {
            txtCardId.Focus();
        }
        private void MemberAdd_Load(object sender, EventArgs e)
        {
            txtCardId.Focus();
        }
        #region Uitilies

        private void MemberSetValue(T_Member tMember)
        {
            SPACommon.SetControlToObjectValue(this,tMember);
            //tMember.CardId = txtCardId.Text.Trim();
            //tMember.CardInsideId = txtCardInsideId.Text.Trim();
            //tMember.Pass = txtPass.Text.Trim();
            //tMember.TreeId = tMember.TreeId;
            //tMember.Name = txtName.Text.Trim();
            //tMember.TelNum = txtTelNum.Text.Trim();
            //tMember.Sex = cmbSex.Text.Trim();
            //tMember.Qq = txtQq.Text.Trim();
            //tMember.Email = txtEmail.Text.Trim();
            //tMember.Fax = txtFax.Text.Trim();
            //tMember.Address = txtAddress.Text.Trim();
            //tMember.AddTime = dtAddTime.Value;
            //tMember.SkinType = cmbSkinType.Text.Trim();
            //tMember.Remark = txtRemark.Text.Trim();
            //tMember.BrithDate = dtBrithDate.Value;
            //tMember.AdminId = cmbAdminId.Text.Trim();
        }

        private void UiSetValue(T_Member tMember)
        {
            SPACommon.SetObjectToControlValue(this, tMember);
            //txtCardId.Text = tMember.CardId;
            //txtCardInsideId.Text = tMember.CardInsideId;
            //txtPass.Text = tMember.Pass;
            //txtTreeId.Text = tMember.TreeId;
            //txtName.Text = tMember.Name;
            //txtTelNum.Text = tMember.TelNum;
            //cmbSex.Text = tMember.Sex;
            //txtQq.Text = tMember.Qq;
            //txtEmail.Text = tMember.Email;
            //txtFax.Text = tMember.Fax;
            //txtAddress.Text = tMember.Address;
            //dtAddTime.Value = tMember.AddTime;
            //cmbSkinType.Text = tMember.SkinType;
            //txtRemark.Text = tMember.Remark;
            //dtBrithDate.Value = tMember.BrithDate;
            //cmbAdminId.ValueMember = tMember.AdminId;
        }

        #endregion

        public void InputWord()
        {
            rchDocument.InputWord();
            SPACommon.SetRichTextBoxExtToControlValue(this, rchDocument);
        }


        private void label11_Click(object sender, EventArgs e)
        {

        }

        public void GetMember(string id)
        {
            var data = LgcMember.GetMemberViewObject(" where T_Member.id = '"+id+"'");
            Tmember = data;
            UiSetValue(data);
        }



        private void btnGetTree_Click(object sender, EventArgs e)
        {
            var formTreeSelecter = new FormTreeSelecter();
            formTreeSelecter.GetTreeByType("MemberTree");
            formTreeSelecter.ShowDialog();
            if (formTreeSelecter.DialogResult != DialogResult.OK) return;
            Tmember.TreeId = formTreeSelecter.TypeId;
            txtTreeName.Text = formTreeSelecter.TypeText;
        }


        private void dtBrithDate_KeyDown(object sender, KeyEventArgs e)
        {
            //BtnSave_Click(null,null);
        }


        private void LblModuleTitle_Click(object sender, EventArgs e)
        {

        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            MemberSetValue(Tmember);
            //rchDocument.SaveFile("");

            if (string.IsNullOrEmpty(Tmember.CardId))
            {
                FormSysMessage.ShowSuccessMsg("会员卡号不能空！");
                return;
            }

            if (string.IsNullOrEmpty(Tmember.TreeId))
            {
                FormSysMessage.ShowSuccessMsg("类型不能空！");
                return;
            }

            if (string.IsNullOrEmpty(Tmember.Name))
            {
                FormSysMessage.ShowSuccessMsg("姓名不能空！");
                return;
            }

            try
            {
                LgcMember.Save(Tmember);
                //if (Tmember.Id == null)
                //{
                //    LgcMember.Add(Tmember);
                //}
                //else
                //{
                //    LgcMember.Update(Tmember);
                //}
            }
            catch (Exception exception)
            {
                MessageBox.Show("私密档案不能包含图片");
                return;
            }
            MemberList.Instance.btnSearch_Click(null, null);
            var findForm = FindForm();
            if (WndMemberAdd != null) WndMemberAdd.Tmember = Tmember;
            if (findForm != null)
            {
                findForm.DialogResult = DialogResult.OK;
                findForm.Close();
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void txtFax_Load(object sender, EventArgs e)
        {

        }

        private void PnlInfoTitle_DragDrop(object sender, DragEventArgs e)
        {

        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }

        private void PnlInfoTitle_Paint(object sender, PaintEventArgs e)
        {
           
        }
    }

}
